//
//  YTKNetworkAgent+MKAgent.m
//  yangtuner
//
//  Created by zhuwh on 2017/4/9.
//  Copyright © 2017年 maikevip. All rights reserved.
//

#import "YTKNetworkAgent+MKAgent.h"
#import <objc/runtime.h>
#if __has_include(<AFNetworking/AFNetworking.h>)
#import <AFNetworking/AFNetworking.h>
#else
#import "AFNetworking.h"
#endif

@implementation YTKNetworkAgent (MKAgent)

+(void)load{
    // 实现init 与 dt_init方法的交换
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL org_Selector = @selector(init);
        SEL mk_Selector  = @selector(mk_init);
        
        Method org_method = class_getInstanceMethod([self class], org_Selector);
        Method mk_method  = class_getInstanceMethod([self class], mk_Selector);
        
        BOOL isAdd = class_addMethod(self, org_Selector, method_getImplementation(mk_method), method_getTypeEncoding(mk_method));
        if (isAdd) {
            class_replaceMethod(self, mk_Selector, method_getImplementation(org_method), method_getTypeEncoding(org_method));
        }else{
            method_exchangeImplementations(org_method, mk_method);
        }
        
        
        SEL org_request_Selector = NSSelectorFromString(@"requestSerializerForRequest:");
        SEL mk_request_Selector  = @selector(mk_requestSerializerForRequest:);
        
        Method org_request_method = class_getInstanceMethod([self class], org_request_Selector);
        Method mk_request_method  = class_getInstanceMethod([self class], mk_request_Selector);
        
        BOOL isRequestAdd = class_addMethod(self, org_request_Selector, method_getImplementation(mk_request_method), method_getTypeEncoding(mk_request_method));
        if (isRequestAdd) {
            class_replaceMethod(self, mk_request_Selector, method_getImplementation(org_request_method), method_getTypeEncoding(org_request_method));
        }else{
            method_exchangeImplementations(org_request_method, mk_request_method);
        }
        
    });
}

-(instancetype)mk_init{
    [self mk_init];
    [self mk_class_copyIvarList:[self class]];
    return self;
}

-(AFHTTPRequestSerializer *)mk_requestSerializerForRequest:(YTKBaseRequest *)request{
    AFHTTPRequestSerializer* requestSerializer = [self mk_requestSerializerForRequest:request];
    //PUT 请求方式的参数，拼接在url之后
    requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", @"DELETE",@"PUT", nil];
    return requestSerializer;
}


- (void)mk_class_copyIvarList:(Class)class {
    unsigned int count;
    NSArray* needIvar = @[@"_jsonResponseSerializer",@"_allStatusCodes"];
    NSMutableDictionary<NSString*,NSValue*>* needIvarDic = [NSMutableDictionary dictionary];
    Ivar *ivarList = class_copyIvarList(class, &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        // 获取成员属性名
        NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
        if (name&&[needIvar containsObject:name]) {
            needIvarDic[name] = [NSValue valueWithBytes:&ivar objCType:@encode(Ivar)];
        }
    }
    
    Ivar jsonResponseSerializerIvar;
    [needIvarDic[@"_jsonResponseSerializer"] getValue:&jsonResponseSerializerIvar];
    Ivar allStatusCodesIvar;
    [needIvarDic[@"_allStatusCodes"] getValue:&allStatusCodesIvar];
    AFJSONResponseSerializer *jsonResponseSerializer = object_getIvar(self, jsonResponseSerializerIvar);
    if (!jsonResponseSerializer) {
        jsonResponseSerializer = [AFJSONResponseSerializer serializer];
        jsonResponseSerializer.acceptableStatusCodes = object_getIvar(self, allStatusCodesIvar);
    }
    jsonResponseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];
    //  修改成员变量的值
    object_setIvar(self, jsonResponseSerializerIvar,jsonResponseSerializer);
    // 记得释放哦
    free(ivarList);
}
@end

